//基础网址
const baseURL1 = "http://127.0.0.1:5002";
const baseURL2 = "http://127.0.0.1:5000";
const baseURL3 = "http://127.0.0.1:5001";
//发布异常(功能二)  post
const post_Result_URL = baseURL2 + "/publish_abnormal";
//视频文件获取(功能一) get
const get_videos_URL = baseURL1 + "/latest_videos";
//异常文本获取(功能一) get
const get_records_URL = baseURL1 + "/anomaly_data";
//发送路线按键(功能三) post
const post_Route_URL = baseURL3 + "/waypoint";

//发布异常按键
function post_Result(collectedTitles) {  // 添加数组参数
  axios({
    method: 'POST',
    url: post_Result_URL,
    data: collectedTitles
  }).then(response => {
    console.log(response)
  }).catch(error => {  // 添加错误处理
    console.error('请求发布异常失败:', error);
  });
}

//视频文件获取
let allVideos = []; // 全局变量
// {
//   page: 1,
//   src01: "https://example.com/video/01工作室.mp4",
//   src02: "https://example.com/video/02工作室.mp4"
// },
async function getAllVideos() {
  try {
    const firstPageData = await get_Videos(1);
    // const totalPages = firstPageData.pageNum;
    const totalPages = firstPageData.page_info.total_pages;
    // 处理第一页数据
    const page1Data = {
      page: 1,
      ...processPageVideos(firstPageData.videos)
    };
    allVideos = [page1Data];
    
    // 处理剩余页数据
    for (let page = 2; page <= totalPages; page++) {
      const pageData = await get_Videos(page);
      const pageObject = {
        page: page,
        ...processPageVideos(pageData.videos)
      };
      allVideos.push(pageObject);
    }
    
    return allVideos;
  } catch (error) {
    console.error('获取所有视频失败:', error);
    throw error;
  }
}
// 处理单页视频数据，生成src01、src02等属性
function processPageVideos(videos) {
  const result = {};
  videos.forEach((video, index) => {
    const srcKey = `src${String(index + 1).padStart(2, '0')}`;
    result[srcKey] = get_videos_URL + video;
  });
  return result;
}
// 修改get_Videos返回Promise
function get_Videos(page) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'GET',
      url: get_videos_URL,
      params: { page: page }
    }).then(response => {
      if (response.data.status === 'success') {
        resolve({
          videos: response.data.video,
          pageNum: response.data.page_info.total_pages
        });
      } else {
        reject(new Error('API返回状态不是success'));
      }
    }).catch(error => {
      reject(error);
    });
  });
}

//异常文本获取
let recordsData = []; // 全局变量
// {
//   "id": 1,
//   "time": "2024-01-15 10:30:25",
//   "lng": 116.3974,    // 经度
//   "lat": 39.9093,      // 纬度  
//   "hight": 50.5,           // 高度
//   "number": 2         // 异常类型
// },
function get_recordsData() {
  axios({
    method: 'GET',
    url: get_records_URL
  }).then(response => {
    console.log('获取异常数据成功:', response.data);
    recordsData = response.data.records; // 更新全局变量
    return response.data;
  }).catch(error => {
    console.error('请求异常数据失败:', error);
    throw error;
  });
}

//发送路线按键
function post_Route(route) {  // 添加数组参数
  axios({
    method: 'POST',
    url: post_Route_URL,
    data: route  
  }).then(response => {
    console.log(response)
  }).catch(error => {  // 添加错误处理
    console.error('请求发送路线失败:', error);
  });
}
